package adminorg;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TreeMap;

import org.vaadin.vaadinvisualizations.LineChart;

public class PaintStatisticLine extends LineChart {
	/**
	 * 
	 */
	private static final long serialVersionUID = -683727434164000364L;
	private DatabaseHelper dbHelp = null;
	private StatisticDataHelper SDHelp = null;
	private Object statUseId = null;
	private PeriodHelper PHelp = null;
	public PaintStatisticLine() {
		// TODO Auto-generated constructor stub
	}

	
	public PaintStatisticLine(DatabaseHelper dbHelper, Object statUseId, Date LastDate, PeriodHelper PHelp, int Width, int Height) throws Exception{
		this();
		this.dbHelp = dbHelper;
		this.SDHelp = new StatisticDataHelper(dbHelper);
		this.PHelp = PHelp;
		this.statUseId = statUseId;
		this.setOption("title", dbHelp.views.getStatisticContainer().getContainerProperty(statUseId, 
				"dictstatistic.name").getValue().toString());
		this.setOption("Width", Width);
		this.setOption("Height", Height);
		this.addXAxisLabel(dbHelp.getWords("Periods"));
		this.addLine(dbHelp.views.getStatisticContainer().getContainerProperty(statUseId, 
				"dictstatistic.code").getValue().toString());
		String PT = new String(dbHelp.views.getStatisticContainer().getContainerProperty(statUseId,
				"aperiodprops.type").getValue().toString());
		SimpleDateFormat df =
			    new SimpleDateFormat("dd.MM.yyyy");
		if (PT.equals("DAILY")){
			TreeMap<Integer,Date> DateValues = new TreeMap<Integer,Date>();
			DateValues = PHelp.getLastPeriodsD(LastDate);
			for (int i=(PHelp.getStatQuant()-DateValues.size()+1);i<=PHelp.getStatQuant();i++){
				this.add(new String(df.format(DateValues.get(i))),new double[]{new Double(SDHelp.getDataD(DateValues.get(i), statUseId))});
			}
		} else {
			TreeMap<Integer,Period> PeriodValues = new TreeMap<Integer,Period>();
			PeriodValues = PHelp.getLastPeriods(LastDate);
			for (int i=(PHelp.getStatQuant()-PeriodValues.size()+1);i<=PHelp.getStatQuant();i++){
				if (PHelp.getPeriodView()){					
					this.add(new String(PeriodValues.get(i).getStringValue()),new double[]{new Double(SDHelp.getDataD(PeriodValues.get(i).getStartDate(), statUseId))});				
				} else {					
					this.add(new String(String.valueOf(PeriodValues.get(i).getNumber())),new double[]{new Double(SDHelp.getDataD(PeriodValues.get(i).getStartDate(), statUseId))});
				}	
			}
		}
		this.setSizeFull();
		this.setImmediate(true);			
	}

}
